package com.codeoss.framework.view.parser;

import java.util.ArrayList;
import java.util.List;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.chart.AbstractChart;
import com.codeoss.framework.view.component.chart.ChartDataItemDef;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public abstract class AbstractChartParser extends AbstractComponentParser {
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		AbstractChart abstractChart = (AbstractChart) component;
		ObjectNode propertyMapNode = (ObjectNode) objectNode.get("propertyMap");
		if (propertyMapNode != null) {
			JsonNode jsonNode = propertyMapNode.get("dataSource");
			if (jsonNode != null) {
				String dataSource = propertyMapNode.get("dataSource").asText();
				abstractChart.setDataSource(dataSource);
			}
			jsonNode = propertyMapNode.get("dimensionList");
			if (jsonNode != null) {
				ArrayNode arrayNode = (ArrayNode) propertyMapNode.get("dimensionList");
				List<ChartDataItemDef> chartDataItemDefList = new ArrayList<ChartDataItemDef>();
				abstractChart.setDimensionList(chartDataItemDefList);
				for (int i = 0; i < arrayNode.size(); i++) {
					ObjectNode defObjectNode = (ObjectNode) arrayNode.get(i);
					ChartDataItemDef def = new ChartDataItemDef();
					chartDataItemDefList.add(def);
					String field = defObjectNode.get("field").asText();
					def.setField(field);
					JsonNode aggregateTypeNode = defObjectNode.get("aggregateType");
					if (aggregateTypeNode != null) {
						def.setAggregateType(aggregateTypeNode.asText());
					}
					JsonNode orderTypeNode = defObjectNode.get("orderType");
					if (orderTypeNode != null) {
						def.setOrderType(orderTypeNode.asText());
					}
					JsonNode fieldExtraNode = defObjectNode.get("fieldExtra");
					if (fieldExtraNode != null) {
						def.setFieldExtra(fieldExtraNode.asText());
					}
				}
			}
			jsonNode = propertyMapNode.get("measureList");
			if (jsonNode != null) {
				ArrayNode arrayNode = (ArrayNode) propertyMapNode.get("measureList");
				List<ChartDataItemDef> chartDataItemDefList = new ArrayList<ChartDataItemDef>();
				abstractChart.setMeasureList(chartDataItemDefList);
				for (int i = 0; i < arrayNode.size(); i++) {
					ObjectNode defObjectNode = (ObjectNode) arrayNode.get(i);
					ChartDataItemDef def = new ChartDataItemDef();
					chartDataItemDefList.add(def);
					String field = defObjectNode.get("field").asText();
					def.setField(field);
					JsonNode aggregateTypeNode = defObjectNode.get("aggregateType");
					if (aggregateTypeNode != null) {
						def.setAggregateType(aggregateTypeNode.asText());
					}
					JsonNode orderTypeNode = defObjectNode.get("orderType");
					if (orderTypeNode != null) {
						def.setOrderType(orderTypeNode.asText());
					}
					JsonNode fieldExtraNode = defObjectNode.get("fieldExtra");
					if (fieldExtraNode != null) {
						def.setFieldExtra(fieldExtraNode.asText());
					}
				}
			}
			jsonNode = propertyMapNode.get("topN");
			if (jsonNode != null) {
				abstractChart.setTopN(jsonNode.asInt());
			}
			jsonNode = propertyMapNode.get("whereClause");
			if (jsonNode != null) {
				String whereClause=jsonNode.asText();
				if(!StringUtils.isEmpty(whereClause)) {
					Expression expression=ExpressionUtil.convertToExpression(whereClause, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					abstractChart.setWhereClauseExpression(expression);
				}
			}
		}

		JsonNode jsonNode = objectNode.get("title");
		if (jsonNode != null) {
			abstractChart.setTitle(jsonNode.asText());
		}
		jsonNode = objectNode.get("subtitle");
		if (jsonNode != null) {
			abstractChart.setSubtitle(jsonNode.asText());
		}
		jsonNode = objectNode.get("col");
		if (jsonNode != null) {
			int col = objectNode.get("col").asInt();
			abstractChart.setCol(col);
		}
		jsonNode = objectNode.get("bigScreen");
		if (jsonNode != null) {
			abstractChart.setBigScreen(jsonNode.asBoolean());
		}
	}
}
